/*
 * Copyright (c) 2009, Hamish Morgan. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * 3. Neither the name of the University of Sussex nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
package piccat.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.RenderingHints;
import java.awt.Stroke;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

/**
 * A Cell is a panel that can hold a PictureView image tile.
 * 
 * @author Hamish Morgan
 */
public class Cell extends JPanel {

    private static final Color FILL_COLOUR = new Color(0, 0, 0, 31);
    private static final Color LINE_COLOUR = new Color(0, 0, 0, 63);
    private static final Stroke LINE_STROKE = new BasicStroke(1);
    private static final Color FILL_COLOUR_TARGET = new Color(255, 0, 0, 63);
    private static final Color LINE_COLOUR_TARGET = new Color(255, 0, 0, 127);

    private static final Border BORDER = LineBorder.createGrayLineBorder();
    private static final Border TARGET_BORDER = new LineBorder(Color.yellow);
    private PictureView contents;
    private boolean rendering;
    private boolean target;

    public Cell() {
        super();
        rendering = true;
        setLayout(new GridLayout(1, 1));
        setOpaque(false);

        contents = null;
        target = false;
    }

    
    public void setContents(PictureView contents) {
        this.contents = contents;
        removeAll();
        add(contents);
        invalidate();
        validate();
    }

    public void setTarget(boolean target) {
        if (target != this.target) {
            this.target = target;
            repaint();
        }
    }

    public boolean isRendering() {
        return rendering;
    }

    public void setRendering(boolean rendering) {
        this.rendering = rendering;
    }

    public boolean isTarget() {
        return target;
    }

    public PictureView getContents() {
        return contents;
    }

    public void clearContents() {
        removeAll();
        contents = null;
    }

    public boolean isContentsSet() {
        return contents != null;
    }
//


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (!isRendering())
            return;


        Graphics2D g2 = (Graphics2D) g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);


        double cx = getWidth() / 2d;
        double cy = getHeight() / 2d;

        double r = Math.min(cx, cy) / 3d;

        double ox = cx - r;
        double oy = cy - r;

        g2.setColor(target ? FILL_COLOUR_TARGET : FILL_COLOUR);
        g2.fillOval((int) ox, (int) oy, (int) (2 * r), (int) (2 * r));
        g2.setStroke(LINE_STROKE);
        g2.setColor(target ? LINE_COLOUR_TARGET : LINE_COLOUR);
        g2.drawOval((int) ox, (int) oy, (int) (2 * r), (int) (2 * r));


    }
}
